import * as sinon from 'sinon';
import { expect } from 'chai';
import { doClone } from '../src/execute-step';
import { IGitSourceParams } from '../src/git-source-params';
import * as retryHelper from '../src/retry-helper';
import * as authUtil from '../src/auth-helper';
import * as step from '@flow-step/step-toolkit';
import * as gitConfigSettings from '../src/git-config-settings';

describe('doClone function - NODEGROUP_TYPE 问题测试', () => {
    let sandbox: sinon.SinonSandbox;
    let updateGitConfigSettingsStub: sinon.SinonStub;
    let retryHelperExecuteStub: sinon.SinonStub;
    let getHostStub: sinon.SinonStub;
    let stepInfoStub: sinon.SinonStub;
    let stepWarningStub: sinon.SinonStub;

    beforeEach(() => {
        sandbox = sinon.createSandbox();
        
        // 存根 updateGitConfigSettings 函数
        updateGitConfigSettingsStub = sandbox.stub(gitConfigSettings, 'updateGitConfigSettings').resolves();
        
        // 存根 retryHelper.execute
        retryHelperExecuteStub = sandbox.stub(retryHelper, 'execute').resolves();
        
        // 存根 authUtil.getHost
        getHostStub = sandbox.stub(authUtil, 'getHost').returns('codeup.aliyun.com');
        
        // 存根 step 方法
        stepInfoStub = sandbox.stub(step, 'info');
        stepWarningStub = sandbox.stub(step, 'warning');
        
        // 清理环境变量
        delete process.env.NODEGROUP_TYPE;
        delete process.env.FLOW_FEATURE_CODEUP_INTERNAL_DOMAIN;
        delete process.env.FLOW_FEATURE_CODEUP_INTERNAL_IP;
    });

    afterEach(() => {
        sandbox.restore();
    });

    describe('NODEGROUP_TYPE 环境变量测试', () => {
        it('当 NODEGROUP_TYPE 不是 K8S 时，不应该调用 updateGitConfigSettings', async () => {
            // 设置环境变量为非 K8S
            process.env.NODEGROUP_TYPE = 'VM';
            
            const input: IGitSourceParams = {
                gitRepo: 'https://codeup.aliyun.com/test/repo.git',
                workDir: '/tmp/test',
                projectDir: '/tmp/test/repo',
                httpUrlWithCredential: true,
                user: 'testuser',
                password: 'testpass',
                httpPolicy: 'https'
            } as IGitSourceParams;

            // 执行 doClone
            await doClone(input);

            // 验证：当 NODEGROUP_TYPE 不是 K8S 时，不应该调用 updateGitConfigSettings
            // 这个测试应该失败，因为当前代码在第341行无条件调用了 updateGitConfigSettings
            expect(updateGitConfigSettingsStub.called).to.be.false;
        });

        it('当 NODEGROUP_TYPE 是 K8S 时，应该调用 updateGitConfigSettings', async () => {
            // 设置环境变量为 K8S
            process.env.NODEGROUP_TYPE = 'K8S';
            process.env.FLOW_FEATURE_CODEUP_INTERNAL_DOMAIN = 'internal.codeup.aliyun.com';
            process.env.FLOW_FEATURE_CODEUP_INTERNAL_IP = '192.168.1.1';
            
            const input: IGitSourceParams = {
                gitRepo: 'https://codeup.aliyun.com/test/repo.git',
                workDir: '/tmp/test',
                projectDir: '/tmp/test/repo',
                httpUrlWithCredential: true,
                user: 'testuser',
                password: 'testpass',
                httpPolicy: 'https'
            } as IGitSourceParams;

            // 执行 doClone
            await doClone(input);

            // 验证：当 NODEGROUP_TYPE 是 K8S 时，应该调用 updateGitConfigSettings
            // 这个测试应该通过，因为 K8S 环境下会调用 updateGitConfigSettings
            expect(updateGitConfigSettingsStub.called).to.be.true;
        });

        it('当 NODEGROUP_TYPE 未设置时，不应该调用 updateGitConfigSettings', async () => {
            // 不设置 NODEGROUP_TYPE 环境变量
            
            const input: IGitSourceParams = {
                gitRepo: 'https://codeup.aliyun.com/test/repo.git',
                workDir: '/tmp/test',
                projectDir: '/tmp/test/repo',
                httpUrlWithCredential: true,
                user: 'testuser',
                password: 'testpass',
                httpPolicy: 'https'
            } as IGitSourceParams;

            // 执行 doClone
            await doClone(input);

            // 验证：当 NODEGROUP_TYPE 未设置时，不应该调用 updateGitConfigSettings
            // 这个测试应该失败，因为当前代码在第341行无条件调用了 updateGitConfigSettings
            expect(updateGitConfigSettingsStub.called).to.be.false;
        });
    });
});
